/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 有点呆
 * Date: 2024-11-07
 * Time: 12:31
 */

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Scanner;

public class TimeAround {

    public static void main(String[] args) {

        ArrayList<Process> list = new ArrayList<>();
        int CPURun = 0;//int CPURun：用于记录CPU的执行时间
        int finishedTime = 0;
        //int finishedTime：进程的运行结束的时间，用于计算周转时间

        System.out.println("请输入进程数：");
        int num = new Scanner(System.in).nextInt();
        int[] serveTimes = new int[num];
        //int[] serveTimes：用于存放进程的时间运行时间，用于计算带权周转时间

        for (int i = 0; i < num; i++) {
            System.out.println("请给第" + (i + 1) + "个进程起个名字：");
            Process initProcess = Process.createInitProcess();
            CPURun += initProcess.getServeTime();
            serveTimes[i] = initProcess.getServeTime();
            initProcess.setIndex(i);
            list.add(initProcess);
        }

        System.out.println("请设置时间片大小(s)：");
        int timeSlice = new Scanner(System.in).nextInt();
        //int timeSlice；定义时间片的大小
        ArrayDeque<Process> deque = new ArrayDeque<Process>(num);
        Process process;
        for (int i = 0; i < CPURun; i++) {
            if (i == 0) {
                System.out.println("第" + i + "秒：");
            }
            //每隔一秒，向队列尾添加一个新的进程
            if (i < num) {
                process = list.get(i);
                deque.offerLast(process);
                System.out.println("进程" + process.getName() + "首次到达就绪队列");
            }
            System.out.println("进程" + deque.getFirst().getName() + "正在执行。。。");

            System.out.println("当前就绪队列情况如下；");
            for (Process task : deque) {
                if (task.getName().equals(deque.getFirst())) {
                    continue;
                }
                System.out.print(task.getName() + "\t");
            }
            System.out.println();
            System.out.println("----------------------------------------");
            if (i != 0) {
                System.out.println("第" + i + "秒：");
            }

            if (i % timeSlice != 0) {
                continue;
            }
            //resetServeTime刷新服务时间，

            Process process1 = deque.pollFirst().resetServeTime(timeSlice);
            if (process1.isAchieved()) {
                System.out.println("进程" + process1.getName() + "执行结束！");
                finishedTime = i + process1.getServeTime();
                System.out.println("进程" + process1.getName() + "的周转时间为：");
                System.out.println(finishedTime - process1.getArriveTime());
                System.out.println("进程" + process1.getName() + "的带权周转时间为：");
                double floatWi =
                        (finishedTime - process1.getArriveTime()) / serveTimes[process1.getIndex()];
                System.out.println(floatWi);
            } else {
                deque.offerLast(process1);
                System.out.println("进程" + process1.getName() + "进入就绪队列");
            }
        }
    }


}


